package main

import (
	"fmt"
	"time"
)

func worker(id int, c chan int) {
	for {
		fmt.Printf("Worker %d received %s\n", id, string(<-c))
	}
}

func createWorker(id int) chan<- int {
	c := make(chan int)
	go worker(id, c)
	return c

}

func chanDemo() {
	var channels [10]chan<- int

	for i := 0; i < 10; i++ {
		channels[i] = createWorker(i)
	}

	for i := 0; i < 10; i++ {
		channels[i] <- 'a' + i
	}

	for i := 0; i < 10; i++ {
		channels[i] <- 'A' + i
	}

}
func bufferWorker(c chan int) {
	go func() {
		for {
			fmt.Printf("bufferWorker:%d\n", <-c)
		}
	}()
}

func bufferChannel() {
	c := make(chan int, 3)
	bufferWorker(c)

	c <- 1
	c <- 2
	c <- 3

}

func channelCloseWorker(c chan int) {
	go func() {
		for value := range c {

			fmt.Printf("bufferWorker:%d\n", value)
		}
	}()
}

func channelClose() {
	c := make(chan int, 3)
	channelCloseWorker(c)

	c <- 1
	close(c)
}

func main() {
	//bufferChannel()
	chanDemo()
	//channelClose()

	time.Sleep(time.Millisecond)

}
